package com.zwk.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.base.entity.CommonResult;
import com.zwk.edu.entity.Course;
import com.zwk.edu.entity.chapter.ChapterVo;
import com.zwk.edu.entity.frontvo.CourseFrontVo;
import com.zwk.edu.entity.frontvo.CourseWebVo;
import com.zwk.edu.entity.vo.CourseInfoVo;
import com.zwk.edu.service.ChapterService;
import com.zwk.edu.service.CourseService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwk
 * @create 2022/8/30 14:33
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/coursefront")
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    // 条件查询带分页查询课程
    @PostMapping("/getFrontCourseList/{current}/{limit}")
    public CommonResult getFrontCourseList(@PathVariable("current") Integer current,
                                           @PathVariable("limit") Integer limit,
                                           @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<Course> page = new Page<>(current, limit);
        Map<String, Object> map = courseService.getCourseFrontList(page, courseFrontVo);
        return CommonResult.ok().data(map);
    }

    // 查询课程详情
    @GetMapping("/getFrontCourseInfo/{id}")
    public CommonResult getFrontCourseInfo(@PathVariable("id") String id){
        // 查询基本信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
//        System.out.println(courseWebVo.toString());

        // 查询章节小节信息
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(id);

        return CommonResult.ok().data("courseWebVo", courseWebVo).data("chapterVideoList", chapterVideoList);
    }

    // 通过课程id获取课程数据, 由其他模块调用
    @PostMapping("/getCourseInfoById")
    public CommonResult getCourseInfoById(@RequestBody String id){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        Map<String, Object> map = new HashMap<>();
        map.put("courseId", baseCourseInfo.getId());
        map.put("courseTitle", baseCourseInfo.getTitle());
        map.put("courseCover", baseCourseInfo.getCover());
        map.put("teacherName", baseCourseInfo.getTeacherName());
        map.put("coursePrice", baseCourseInfo.getPrice());
        return CommonResult.ok().data(map);
    }

}
